package com.irdstudio.apicenter.frame.maker;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.irdstudio.apicenter.console.util.CurrentDateUtil;
import com.irdstudio.apicenter.core.dvo.RspValue;
import com.irdstudio.apicenter.frame.core.jdbc.JdbcBaseDAO;
import com.irdstudio.apicenter.frame.core.jdbc.container.DataList;

/**
 * 按模块来制作与生成代码
 * @author guangming.li
 * @version 1.0
 * @date 2015-12-22
 */
public class ModuleMaker {
	
	private JdbcBaseDAO jdbcBaseDAO;
	private String appId;
	private String moduleId;
	private final Logger logger = LoggerFactory.getLogger(ModuleMaker.class);

	public ModuleMaker(JdbcBaseDAO jdbcBaseDAO, String appId, String moduleId) {
		this.jdbcBaseDAO = jdbcBaseDAO;
		this.appId = appId;
		this.moduleId = moduleId;
	}
	
	public ModuleMaker(JdbcBaseDAO jdbcBaseDAO, String appId) {
		this.jdbcBaseDAO = jdbcBaseDAO;
		this.appId = appId;
	}

	/**
	 * 将指定应用下指定模块的代码生成
	 * @param jdbcBaseDAO 
	 * @param appId
	 * @param moduleId
	 * @return
	 * @throws IOException 
	 */
	public RspValue make() throws IOException {

		// 读取模块下的参数集合
		DataList moduleParamList = new DataList(
				jdbcBaseDAO
						.findMapList(
								"select * from app_module_param where app_id=? and module_id=?",
								new Object[] { appId, moduleId }));

		// 读取模块下的页面集合
		DataList modulePageList = new DataList(jdbcBaseDAO
				.findMapList("select * from s_module_page where module_id=?",
						new Object[] { moduleId }));

		// 生成模块下页面代码
		boolean flag = true;
		for (int i = 0; i < modulePageList.size(); i++) {
			String pageId = modulePageList.get(i).getValue("page_id");
			this.logger.info("开始生成模块[" + moduleId + "]下的页面[" + pageId
					+ "]代码...");
			RspValue rspValue = new PageMaker(jdbcBaseDAO,pageId, moduleParamList).make(appId);
			flag = rspValue.isOK() ? flag : false;
		}

		if (flag) {

			// 生成代码成功,更新APP的应用工程状态及模块的代码生成日期
			if (!updateModuleState(appId, moduleId)) {
				return new RspValue("更新应用工程状态及日期出错!");
			}

			// 将代码文件提交到SVN上
			return new RspValue();

		} else {

			// 生成模块代码失败,返回失败信息
			return new RspValue("生成模块代码失败!");
		}
	}
	
	/**
	 * 生成指定页面代码
	 * @param jdbcBaseDAO 
	 * @param pageId
	 * @return
	 * @throws IOException 
	 */
	public RspValue createPageCode(String pageId) throws IOException {

		// 生成页面代码
		this.logger.info("开始生成页面[" + pageId + "]代码...");
		RspValue rspValue = new PageMaker(jdbcBaseDAO, pageId).createPageCode(appId);
		return rspValue;
	}
	
	/**
	 * 更新应用工程状态及模块代码生成日期
	 * 
	 * @param appId
	 * @param moduleId
	 * @return
	 */
	private boolean updateModuleState(String appId, String moduleId) {
		int rst1 = jdbcBaseDAO
				.update(
						"update app_base_info set app_project_state='2',app_project_date=? where app_id=?",
						new Object[] { CurrentDateUtil.getTodayDate(), appId });
		int rst2 = jdbcBaseDAO
				.update(
						"update app_frame_module set last_code_date=? where app_id=? and module_id=?",
						new Object[] { CurrentDateUtil.getTodayDate(), appId,
								moduleId });
		return rst1 == 1 && rst2 == 1;
	}
}
